package com.thread.concurrent.executorservice;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Demo6 {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService pool = Executors.newFixedThreadPool(10);
		CompletionService<String> completionService = new ExecutorCompletionService<String>(pool);
		for (int i = 0; i < 10; i++) {
			completionService.submit(new Callable<String>() {

				public String call() throws Exception {
					return Thread.currentThread().getName();
				}
			});
		}
		
		for (int i = 0; i < 10; i++) {
			Future<String> result = completionService.take();
			System.out.println(result.get());
		}
		
		pool.shutdown();
	}
}
